/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.AdminMapper;
import com.ybkj.daijia.server.mapper.AdminRoleMapper;
import com.ybkj.daijia.server.mapper.CompanyMapper;
import com.ybkj.daijia.server.mapper.RoleMapper;
import com.ybkj.daijia.server.model.Admin;
import com.ybkj.daijia.server.model.AdminRole;
import com.ybkj.daijia.server.service.AdminService;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Shine
 *
 */
@Service
public class DefaultAdminService implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private CompanyMapper companyMapper;

    /*
     * (non-Javadoc)
     *
     * @see com.ybkj.shop.service.AdminService#queryAll()
     */
    public Page<Admin> selectByPage(final Long companyId, final String name) {

        List<Admin> admins = adminMapper
            .selectByUsernameLike(companyId, name, Pager.getOffset(), Pager.getLimit());

        if (!CollectionUtils.isEmpty(admins)) {
            for (Admin admin : admins) {
                admin.setCompany(companyMapper.selectByPrimaryKey(admin.getCompanyId()));
                admin.setRoles(roleMapper.selectByAdminId(admin.getId()));
            }
        }

        long total = adminMapper.countByUsernameLike(companyId, name);

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        Page<Admin> page = new PageImpl<Admin>(admins, pageable, total);

        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    public Admin insertAdmin(Admin admin, Long[] roleIds) {

        // 保存管理员信息
        adminMapper.insertRecord(admin);
        // 保存管理员角色关联信息
        if (ArrayUtils.isNotEmpty(roleIds)) {
            List<AdminRole> adminRoles = new LinkedList<AdminRole>();
            for (Long roleId : roleIds) {
                AdminRole adminRole = new AdminRole(admin.getId(), roleId);
                adminRoles.add(adminRole);
            }
            adminRoleMapper.batchInsertRecord(adminRoles);
        }

        return admin;
    }

    public Admin findOne(Long id) {

        return adminMapper.selectByPrimaryKey(id);
    }

    public Admin findByUsername(String username) {

        return adminMapper.selectByUsername(username);
    }

    public List<Admin> findByIdIn(Long[] ids) {

        return adminMapper.selectByIdIn(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Admin admin) {
        // 删除管理员与角色的关联
        adminRoleMapper.deleteByAdminId(admin.getId());
        // 删除管理员
        adminMapper.deleteByPrimaryKey(admin.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long updateByPrimaryKey(Admin admin) {

        return adminMapper.updateByPrimaryKey(admin);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByPrimaryKey(Admin admin, Long[] roleIds) {

        long adminId = admin.getId();
        // 修改管理员信息
        adminMapper.updateByPrimaryKey(admin);
        // 删除管理员与角色关联信息
        adminRoleMapper.deleteByAdminId(adminId);

        // 保存管理员角色关联信息
        if (ArrayUtils.isNotEmpty(roleIds)) {
            List<AdminRole> adminRoles = new LinkedList<AdminRole>();
            for (Long roleId : roleIds) {
                AdminRole adminRole = new AdminRole(adminId, roleId);
                adminRoles.add(adminRole);
            }
            adminRoleMapper.batchInsertRecord(adminRoles);
        }

    }

    @Override
    public Admin findOneWithRoles(Long id) {

        return adminMapper.selectWithRoles(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVcode(Admin admin) {
        adminMapper.updateVcode(admin);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addLoginFailed(Long id) {
        adminMapper.addLoginFailed(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void lockUser(Long id) {
        adminMapper.lockUser(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unLockUser(Long id) {
        adminMapper.unLockUser(id);
    }

    @Override
    public Integer selectMessageTime(String userName) {
        return adminMapper.selectMessageTime(userName);
    }

    @Override
    public void setMessageTime(String userName, Integer messageTime) {
        adminMapper.setMessageTime(userName, messageTime);
    }

    @Override
    public void shujuliucun() {

    }

    ;
}